﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WF.Model;
using WF.DAL;
using WF.Common;

namespace WF.BusinessObject
{
    public class KQApply : BOBase<WF.Model.KQApplyEntity, DAL.KQApply>
    {
        #region 构造方法
        public KQApply() : base() { }
        public KQApply(WFDBDataContext DataContext) : base(DataContext) { }
        #endregion

        #region 实现基类abstract方法
        /// <summary>
        /// 实现基类的GetById abstract方法
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public override KQApplyEntity GetById(string ID)
        {
            var obj = DataContext.KQApplies.SingleOrDefault(o => o.RowID == ID);
            return new KQApplyEntity(obj);
        }
        #endregion

        #region Public 方法
        public List<WF.Model.KQApplyEntity> GetAll()
        {
            var target = DataContext.KQApplies.ToList();
            List<KQApplyEntity> _result = new List<KQApplyEntity>();
            foreach (var item in target)
            {
                _result.Add(new KQApplyEntity(item));
            }
            return _result;
        }

        /// <summary>
        /// 按条件查询申请单
        /// </summary>
        /// <param name="folio">申请单信息</param>
        /// <param name="holidayBeginDate">查询开始时间</param>
        /// <param name="holidayEndDate">查询结束时间</param>
        /// <param name="flowState">申请单状态</param>
        /// <param name="isMine">是否本人</param>
        /// <returns></returns>
        public List<WF.Model.KQApplyEntity> GetAllByCondition(string folio, string BeginDate, string EndDate, int? flowState, bool isMine, bool isCanQuery)
        {
            string EmpID = "";
            if (string.IsNullOrEmpty(folio))
                folio = string.Empty;
            if (string.IsNullOrEmpty(BeginDate))
                BeginDate = DateTime.MinValue.ToString();
            if (string.IsNullOrEmpty(EndDate))
                EndDate = DateTime.MinValue.ToString();
            if (flowState == null)
            {
                flowState = -2;
            }
            if (isMine)
                EmpID = SessionManager.CurrentUser.LoginName;
            if (string.IsNullOrEmpty(EmpID))
            {
                EmpID = string.Empty;
            }

            List<KQApplyEntity> rt = new List<KQApplyEntity>();
            if (isCanQuery)
            {
                var obj = from d in DataContext.KQApplies
                          where (string.IsNullOrEmpty(folio) ? true : d.Folio.Contains(folio)) &&
                          (DateTime.MinValue.ToString().Equals(BeginDate) ? true : d.ApplyRegisterDate >= DateTime.Parse(BeginDate)) &&
                          (DateTime.MinValue.ToString().Equals(EndDate) ? true : d.ApplyRegisterDate < DateTime.Parse(EndDate).AddDays(1)) &&
                          (flowState == -2 ? true : flowState == d.FlowState) &&
                          (string.IsNullOrEmpty(EmpID) ? true : EmpID == d.ApplyAccount)
                          orderby d.ApplyRegisterDate descending
                          select d;

                foreach (var item in obj)
                {
                    rt.Add(new KQApplyEntity(item));
                }
            }
            return rt;
        }
        #endregion
    }
}
